/**
 * Copyright (c) 2014-2015 ENZO 熊强强  (xqenzo@163.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 	
 */

package cn.edu.xyc.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import cn.edu.xyc.domain.EntityPojo;

/**
 * <p>
 * Title: XlsUtils.java
 * </p>
 * <p>
 * Description:用于来读取
 * </p>
 * <p>
 * Copyright: Copyright (c) 2015
 * </p>
 * 
 * @author ENZO E-mail:xqenzo@163.com
 * @date 创建时间：2015年10月18日 上午9:44:49
 * @version 1.0
 */
public class XlsUtils {

    /**
     * xls 的文件的ContentType 类型：application/vnd.ms-excel
     */
    public static final String XLS_CONTENTTYPE = "application/vnd.ms-excel";

    /**
     * xlsx 的文件的ContentType
     * 类型：application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
     */
    public static final String XLS_XCONTENTTYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    @SuppressWarnings("resource")
    public static List<EntityPojo> readXls(String filepath) throws IOException {
	List<EntityPojo> entityList = new ArrayList<EntityPojo>();

	InputStream is = new FileInputStream(filepath);
	HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

	// 循环工作表Sheet
	for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
	    HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
	    if (hssfSheet == null) {
		continue;
	    }

	    // 循环行Row
	    for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
		HSSFRow hssfRow = hssfSheet.getRow(rowNum);
		if (hssfRow == null) {
		    continue;
		}

		// 循环列Cell
		// for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum();
		// cellNum++) {
		// HSSFCell hssfCell = hssfRow.getCell(cellNum);
		// if (hssfCell == null) {
		// continue;
		// }
		EntityPojo entityPojo = new EntityPojo();
		entityPojo.setIdcard(getValue(hssfRow.getCell(0)));
		entityPojo.setComanyname(getValue(hssfRow.getCell(1)));
		entityPojo.setCount(getValue(hssfRow.getCell(2)));
		entityPojo.setMoney(getValue(hssfRow.getCell(3)));
		entityPojo.setName(getValue(hssfRow.getCell(4)));
		// }

		entityList.add(entityPojo);
		// System.out.println();
	    }
	}
	return entityList;
    }

    @SuppressWarnings("resource")
    public static List<EntityPojo> readXlsx(String filepath) throws IOException {
	XSSFWorkbook xssfWorkbook = new XSSFWorkbook(filepath);
	List<EntityPojo> entityList = new ArrayList<EntityPojo>();

	// 循环工作表Sheet
	for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
	    XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
	    if (xssfSheet == null) {
		continue;
	    }

	    // 循环行Row
	    for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
		XSSFRow xssfRow = xssfSheet.getRow(rowNum);
		if (xssfRow == null) {
		    continue;
		}

		// 循环列Cell
		EntityPojo entityPojo = new EntityPojo();
		entityPojo.setIdcard(getValue(xssfRow.getCell(0)));
		entityPojo.setComanyname(getValue(xssfRow.getCell(1)));
		entityPojo.setCount(getValue(xssfRow.getCell(2)));
		entityPojo.setMoney(getValue(xssfRow.getCell(3)));
		entityPojo.setName(getValue(xssfRow.getCell(4)));
		entityList.add(entityPojo);
	    }
	}
	return null;
    }

    @SuppressWarnings("static-access")
    private static String getValue(HSSFCell hssfCell) {
	if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
	    return String.valueOf(hssfCell.getBooleanCellValue());
	} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
	    return String.valueOf(hssfCell.getNumericCellValue());
	} else {
	    return String.valueOf(hssfCell.getStringCellValue());
	}
    }

    @SuppressWarnings("static-access")
    private static String getValue(XSSFCell xssfCell) {
	if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) {
	    return String.valueOf(xssfCell.getBooleanCellValue());
	} else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) {
	    return String.valueOf(xssfCell.getNumericCellValue());
	} else {
	    return String.valueOf(xssfCell.getStringCellValue());
	}
    }
}
